﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml.Linq;

namespace RouterTool
{
  public partial class RuleTool : Form
  {
    public RuleTool()
    {
      InitializeComponent();
    }

    private void buttAdd_Click(object sender, EventArgs e)
    {
      if (comboFromURI.Text == "Select...")
      {
        MessageBox.Show("Choose From URI...");
        return;
      }
      else if (comboFromValue.Text == "Select...")
      {
        MessageBox.Show("Choose From Value...");
        return;
      }
      else if (comboToURI.Text == "Select..." && comboType.Text != "ContentKeywordFed")
      {
        MessageBox.Show("Choose To URI...");
        return;
      }
      else if (comboToValue.Text == "Select..." && comboType.Text!="ContentKeywordFed")
      {
        MessageBox.Show("Choose To Value...");
        return;
      }
      else if (comboType.Text == "Select...")
      {
        MessageBox.Show("Choose Type...");
        return;
      }
      RouterRulesLiNQDataContext linqdb = new RouterRulesLiNQDataContext();
      Rule entry = new Rule();
      entry.FromListURI = comboFromURI.Text;
      entry.FromListValue = comboFromValue.Text;
      entry.RuleKey = Guid.NewGuid();
      entry.RuleType = comboType.Text;
      if (comboType.Text == "ContentKeywordFed")
      {
        try
        {
          Uri test = new Uri(touri_tb.Text); ////Test to see if valid URI
          entry.ToListURI = touri_tb.Text;
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString(), "URI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
        }
      }
      else
      {
        entry.ToListURI = comboToURI.Text;
        entry.ToListValue = comboToValue.Text;
      }
      linqdb.Rules.InsertOnSubmit(entry);
      linqdb.SubmitChanges();
      MessageBox.Show("Done");
    }

    private void buttRefresh_Click(object sender, EventArgs e)
    { 
      ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
      var uris = (from p in linqdb.IC_NETValueLists
                  select p.ValueListURI).Distinct();
      comboToURI.Items.Clear();
      comboFromURI.Items.Clear();
      comboToURI.Text = "Select...";
      comboFromURI.Text = "Select...";
      foreach (string uri in uris)
      {
        comboToURI.Items.Add(uri);
        comboFromURI.Items.Add(uri);
      }      
    }

    private void RefreshFromValue()
    {
      /*HttpWebRequest request;
      HttpWebResponse resp;
      Stream resStream;
      XElement xe;*/
      if (comboFromURI.Text != "Select...")
      {
        /*request = (HttpWebRequest)WebRequest.Create(comboFromURI.Text);
        resp = (HttpWebResponse)request.GetResponse();
        resStream = resp.GetResponseStream();
        xe = XElement.Load(resStream);
        resStream.Close();
        var values = from p in xe.Descendants("Value")
                     select p;
        comboFromValue.Items.Clear();
        foreach (XElement ele in values)
        {
          comboFromValue.Items.Add(ele.Value);
        }*/
        try
        {
          ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
          var values = (from p in linqdb.IC_NETValueLists
                        where p.ValueListURI == comboFromURI.Text
                        select p.Value);
          comboFromValue.Items.Clear();
          comboFromValue.Text = "Select...";
          foreach (string value in values)
          {
            comboFromValue.Items.Add(value);
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString(), "Error Getting Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

      }
    }

    private void RefreshToValue()
    {
      /*HttpWebRequest request;
      HttpWebResponse resp;
      Stream resStream;
      XElement xe;*/
      if (comboToURI.Text != "Select...")
      {
        /*request = (HttpWebRequest)WebRequest.Create(comboToURI.Text);
        resp = (HttpWebResponse)request.GetResponse();
        resStream = resp.GetResponseStream();
        xe = XElement.Load(resStream);
        resStream.Close();
        var values2 = from p in xe.Descendants("Value")
                      select p;
        comboToValue.Items.Clear();
        foreach (XElement ele in values2)
        {
          comboToValue.Items.Add(ele.Value);
        }*/

        try
        {
          ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
          var values = (from p in linqdb.IC_NETValueLists
                        where p.ValueListURI == comboToURI.Text
                        select p.Value);
          comboToValue.Items.Clear();
          comboToValue.Text = "Select...";
          foreach (string value in values)
          {
            comboToValue.Items.Add(value);
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString(), "Error Getting Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }
    }

    private void RuleTool_Load(object sender, EventArgs e)
    {
      buttRefresh_Click(this, e);
    }

    private void comboFromURI_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.RefreshFromValue();
    }

    private void comboToURI_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.RefreshToValue();
    }

    private void comboType_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (comboType.Text == "ContentKeywordFed")
      {
        touri_tb.Enabled = true;
        comboToURI.Enabled = false;
        comboToValue.Enabled = false;
      }
      else
      {
        touri_tb.Enabled = false;
        comboToURI.Enabled = true;
        comboToValue.Enabled = true;
      }
    }
  }
}
